package cn.bdqn.page2;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

/**
 * 办理退网
 */
public class Delete {
	// 因为所有的方法都需要DOM树
	static Document document = null;
	static Element root = null;

	// 保存用户信息类
	public static void save(String path) {
		OutputFormat format = null;
		XMLWriter writer = null;
		try {
			format = OutputFormat.createPrettyPrint();
			writer = new XMLWriter(new FileWriter("UserMessage.xml"), format);
			writer.write(document);

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				writer.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	// 注销账户
	public void delete(String phone) throws DocumentException {
		SAXReader reader = new SAXReader();
		document = reader.read("UserMessage.xml");
		root = (Element) document.getRootElement();
		Iterator itUser = root.elementIterator();
		while (itUser.hasNext()) {
			Element user = (Element) itUser.next();
			if (user.attributeValue("id").equals(phone)) {
				user.getParent().remove(user);
			}
		}
		save("UserMessage.xml");
		System.out.println("办理退网成功！！");

	}
}
